package com.community.web.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.common.exception.NeusoftException;
import com.community.common.result.ResultCodeEnum;
import com.community.model.entity.PortalWalletLog;
import com.community.model.entity.User;
import com.community.model.entity.Wallet;
import com.community.web.user.service.LoginService;
import com.community.web.user.service.PortalWalletLogService;
import com.community.web.user.service.UserService;
import com.community.web.user.service.WalletService;
import com.community.web.user.mapper.WalletMapper;
import com.community.web.user.vo.TransferVo;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
* @author admin
* @description 针对表【wallet】的数据库操作Service实现
* @createDate 2024-07-02 10:56:56
*/
@Service
public class WalletServiceImpl extends ServiceImpl<WalletMapper, Wallet>
    implements WalletService{

    @Autowired
    private PortalWalletLogService logService;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private LoginService loginService;

    @Autowired
    private WalletMapper mapper;

    @Override
    public Wallet select(Long userId, String password) {
        Wallet wallet = mapper.getByUserId(userId);
        if (wallet == null){
            throw new NeusoftException(ResultCodeEnum.USER_ACCOUNT_NOT_EXIST_ERROR);
        }
        if (wallet.getWalletPassword() == null){
            throw new NeusoftException(ResultCodeEnum.EMPTY_PASSWORD);
        }
        if (!wallet.getWalletPassword().equals(DigestUtils.md5Hex(password))){
            throw new NeusoftException(ResultCodeEnum.PASSWORD_ERROR);
        }
        return wallet;
    }

    @Transactional
    @Override
    public void addMoney(Long userId, BigDecimal money) {
        Wallet wallet = mapper.getByUserId(userId);
        wallet.setWalletBalance(wallet.getWalletBalance().add(money));
        super.saveOrUpdate(wallet);
        //添加记录
        PortalWalletLog walletLog = new PortalWalletLog();
        walletLog.setWalletId(wallet.getId());
        walletLog.setType(2);
        walletLog.setState(1);
        walletLog.setAmount(money);
        logService.addLog(walletLog);
    }

    @Transactional
    @Override
    public void transfer(Long userId, TransferVo transferVo) {
        //判断验证码
        if (transferVo.getCode() == null){
            throw new NeusoftException(ResultCodeEnum.USER_LOGIN_CODE_EMPTY);
        }
        User user = userService.getById(userId);
        loginService.checkCode(transferVo.getCode(), user.getPhone());
        //判断余额是否充足
        Wallet wallet = mapper.getByUserId(userId);
        if (wallet.getWalletBalance().compareTo(transferVo.getMoney()) < 0){
            throw new NeusoftException(ResultCodeEnum.NOT_ENOUGH);
        }
        //减少当前用户余额
        wallet.setWalletBalance(wallet.getWalletBalance().subtract(transferVo.getMoney()));
        super.saveOrUpdate(wallet);
        //增加user余额
        Wallet wallet1 = mapper.getByUserId(user.getId().longValue());
        wallet1.setWalletBalance(wallet.getWalletBalance().add(transferVo.getMoney()));
        super.saveOrUpdate(wallet1);
        //添加记录
        //转出
        PortalWalletLog walletLog = new PortalWalletLog();
        walletLog.setWalletId(wallet.getId());
        walletLog.setAmount(transferVo.getMoney());
        walletLog.setType(4);
        walletLog.setState(1);
        logService.addLog(walletLog);
        //转入
        PortalWalletLog walletLog1 = new PortalWalletLog();
        walletLog1.setWalletId(wallet1.getId());
        walletLog1.setAmount(transferVo.getMoney());
        walletLog1.setType(3);
        walletLog1.setState(1);
        logService.addLog(walletLog1);
    }

    @Override
    public void sendCodeById(Long userId) {
        User user = userService.getById(userId);
        loginService.sendCode(user.getPhone());
    }
}




